/*
 * 微信公众平台(JAVA) SDK
 *
 * Copyright (c) 2014, Ansitech Network Technology Co.,Ltd All rights reserved.
 * 
 * http://www.weixin4j.org/sdk/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.wss.common.wechat.massage;

import com.wss.common.wechat.massage.app.AuthInputMessage;
import com.wss.common.wechat.massage.app.TicketInputMessage;
import com.wss.common.wechat.massage.normal.NormalMessage;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;


/**
 * POST的XML数据包转换为消息接受对象
 * <p>
 * <p>
 * 由于POST的是XML数据包，所以不确定为哪种接受消息，<br/>
 * 所以直接将所有字段都进行转换，最后根据<tt>MsgType</tt>字段来判断取何种数据</p>
 * 所以直接将所有字段都进行转换，最后根据<tt>MsgInfo</tt>字段来判断取何种数据</p>
 *
 * @author weixin4j<weixin4j@ansitech.com>
 */
@XmlRootElement(name = "xml")
public class InputMessage{

	/**
	 * 开发者微信号
	 */
	private String ToUserName;
	/**
	 * 发送方帐号（一个OpenID）
	 */
	private String FromUserName;
	/**
	 * 消息id，64位整型
	 */
	private Long MsgId;
	/**
	 * 消息来源
	 */
	private String AppId;

	/**
	 * 消息创建时间 （整型）
	 */
	private Long CreateTime;

	private String MsgType;
	private String InfoType;
	// 文本消息
	private String Content;

	// 事件
	private String Event;
	private String EventKey;
	private String Ticket;

	//群发消息事件
	private String MsgID;
	private String Status;
	private int TotalCount;
	private int FilterCount;
	private int SentCount;
	private int ErrorCount;

	//授权消息事件
	private String ComponentVerifyTicket;
	private String AuthorizerAppid;
	private String AuthorizationCode;
	private Long AuthorizationCodeExpiredTime;



	public String getMsgType() {
		return MsgType;
	}
	public String getInfoType() {
		return InfoType;
	}

	public String getToUserName() {
		return ToUserName;
	}

	@XmlElement(name = "ToUserName")
	public void setToUserName(String ToUserName) {
		this.ToUserName = ToUserName;
	}

	public String getFromUserName() {
		return FromUserName;
	}

	@XmlElement(name = "FromUserName")
	public void setFromUserName(String FromUserName) {
		this.FromUserName = FromUserName;
	}

	public Long getMsgId() {
		return MsgId;
	}

	@XmlElement(name = "MsgId")
	public void setMsgId(Long MsgId) {
		this.MsgId = MsgId;
	}

	public Long getCreateTime() {
		return CreateTime;
	}

	@XmlElement(name = "CreateTime")
	public void setCreateTime(Long CreateTime) {
		this.CreateTime = CreateTime;
	}

	public String getAppId() {
		return AppId;
	}
	@XmlElement(name = "AppId")
	public void setAppId(String appId) {
		AppId = appId;
	}

	@XmlElement(name = "MsgType")
	public void setMsgType(String msgType) {
		MsgType = msgType;
	}

	@XmlElement(name = "InfoType")
	public void setInfoType(String infoType) {
		InfoType = infoType;
	}

	public String getContent() {
		return Content;
	}

	@XmlElement(name = "Content")
	public void setContent(String content) {
		Content = content;
	}

	public String getEvent() {
		//转成小写
		return Event.toLowerCase();
	}

	@XmlElement(name = "Event")
	public void setEvent(String event) {
		Event = event;
	}

	public String getEventKey() {
		return EventKey;
	}

	@XmlElement(name = "EventKey")
	public void setEventKey(String eventKey) {
		EventKey = eventKey;
	}


	public String getTicket() {
		return Ticket;
	}

	@XmlElement(name = "Ticket")
	public void setTicket(String ticket) {
		Ticket = ticket;
	}

	public String getStatus() {
		return Status;
	}

	@XmlElement(name = "Status")
	public void setStatus(String Status) {
		this.Status = Status;
	}

	public int getTotalCount() {
		return TotalCount;
	}

	@XmlElement(name = "TotalCount")
	public void setTotalCount(int TotalCount) {
		this.TotalCount = TotalCount;
	}

	public int getFilterCount() {
		return FilterCount;
	}

	@XmlElement(name = "FilterCount")
	public void setFilterCount(int FilterCount) {
		this.FilterCount = FilterCount;
	}

	public int getSentCount() {
		return SentCount;
	}

	@XmlElement(name = "SentCount")
	public void setSentCount(int SentCount) {
		this.SentCount = SentCount;
	}

	public int getErrorCount() {
		return ErrorCount;
	}

	@XmlElement(name = "ErrorCount")
	public void setErrorCount(int ErrorCount) {
		this.ErrorCount = ErrorCount;
	}

	public String getMsgID() {
		return MsgID;
	}

	@XmlElement(name = "MsgID")
	public void setMsgID(String MsgID) {
		this.MsgID = MsgID;
	}

	public String getComponentVerifyTicket() {
		return ComponentVerifyTicket;
	}
	@XmlElement(name = "ComponentVerifyTicket")
	public void setComponentVerifyTicket(String componentVerifyTicket) {
		ComponentVerifyTicket = componentVerifyTicket;
	}

	public String getAuthorizerAppid() {
		return AuthorizerAppid;
	}
	@XmlElement(name = "AuthorizerAppid")
	public void setAuthorizerAppid(String authorizerAppid) {
		AuthorizerAppid = authorizerAppid;
	}

	public String getAuthorizationCode() {
		return AuthorizationCode;
	}
	@XmlElement(name = "AuthorizationCode")
	public void setAuthorizationCode(String authorizationCode) {
		AuthorizationCode = authorizationCode;
	}

	public Long getAuthorizationCodeExpiredTime() {
		return AuthorizationCodeExpiredTime;
	}
	@XmlElement(name = "AuthorizationCodeExpiredTime")
	public void setAuthorizationCodeExpiredTime(Long authorizationCodeExpiredTime) {
		AuthorizationCodeExpiredTime = authorizationCodeExpiredTime;
	}


	private void initNormalMessage(NormalMessage inputMessage) {
		inputMessage.setToUserName(this.getToUserName());
		inputMessage.setFromUserName(this.getFromUserName());
		inputMessage.setMsgId(this.getMsgId());
		inputMessage.setCreateTime(this.getCreateTime());
	}

	public TicketInputMessage toTicketInputMessage() {
		return new TicketInputMessage(ComponentVerifyTicket);
	}

	public AuthInputMessage toAuthInputMessage() {
		AuthInputMessage authInputMessage = new AuthInputMessage();
		authInputMessage.setAuthorizerAppId(AuthorizerAppid);
		authInputMessage.setAuthorizationCode(AuthorizationCode);
		return authInputMessage;
	}
}
